<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户姓名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="用户性别" prop="userSex">
      <el-input v-model="dataForm.userSex" placeholder="用户性别"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="收货地址ID" prop="addressId">
      <el-input v-model="dataForm.addressId" placeholder="收货地址ID"></el-input>
    </el-form-item>
    <el-form-item label="商品ID" prop="shopId">
      <el-input v-model="dataForm.shopId" placeholder="商品ID"></el-input>
    </el-form-item>
    <el-form-item label="余额" prop="userPrice">
      <el-input v-model="dataForm.userPrice" placeholder="余额"></el-input>
    </el-form-item>
    <el-form-item label="管理权限" prop="userAuma">
      <el-input v-model="dataForm.userAuma" placeholder="管理权限"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userName: '',
          userSex: '',
          userPhone: '',
          addressId: '',
          shopId: '',
          userPrice: '',
          userAuma: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          userSex: [
            { required: true, message: '用户性别不能为空', trigger: 'blur' }
          ],
          userPhone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          addressId: [
            { required: true, message: '收货地址ID不能为空', trigger: 'blur' }
          ],
          shopId: [
            { required: true, message: '商品ID不能为空', trigger: 'blur' }
          ],
          userPrice: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ],
          userAuma: [
            { required: true, message: '管理权限不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/commerce/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.userName
                this.dataForm.userSex = data.user.userSex
                this.dataForm.userPhone = data.user.userPhone
                this.dataForm.addressId = data.user.addressId
                this.dataForm.shopId = data.user.shopId
                this.dataForm.userPrice = data.user.userPrice
                this.dataForm.userAuma = data.user.userAuma
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/commerce/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'userSex': this.dataForm.userSex,
                'userPhone': this.dataForm.userPhone,
                'addressId': this.dataForm.addressId,
                'shopId': this.dataForm.shopId,
                'userPrice': this.dataForm.userPrice,
                'userAuma': this.dataForm.userAuma
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
